﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using System.Security.Principal;


namespace SecurityAgent.Client.WinForm
{
    using LoginEventHandler = EventHandler<LoginEventArgs>;
   
    

    [DefaultEvent("LoginEvent")]
    public partial class LoginControl : UserControl
    {
        public LoginControl()
        {
            InitializeComponent();
        }

        string m_ApplicationName = String.Empty;
        bool m_CacheRoles = false;
        public event LoginEventHandler LoginEvent = delegate { };

        [Category("Credentials")]
        public bool CacheRoles //Gets and sets m_CacheRoles
        {
            get
            {
                return m_CacheRoles;
            }
            set
            {
                m_CacheRoles = value;
            }
        }

        [Category("Credentials")]
        public bool CacheClaims //Gets and sets m_CacheRoles
        {
            get
            {
                return m_CacheRoles;
            }
            set
            {
                m_CacheRoles = value;
            }
        }

       
        [Category("Credentials")]
        public string ApplicationKey //Gets and sets m_ ApplicationName
        {
            get
            {
                return m_ApplicationName;
            }
            set
            {
                m_ApplicationName = value;
            }
        }

        static public void Logout()
        {
            SASPrincipal customPrincipal =
               Thread.CurrentPrincipal as SASPrincipal;
            if (customPrincipal != null)
            {
                customPrincipal.Detach();
            }
        }

        static public bool IsLoggedIn
        {
            get { return Thread.CurrentPrincipal is SASPrincipal; }
        }

        protected virtual void OnLogin(object sender, EventArgs e)
        {
            string userName = txtUserName.Text;
            string password = txtPassword.Text;

            /* Validation of userName and password using the error provider */

            IUserManager userManager = new SASUserManager();

            bool authenticated;

            string userToken;
            authenticated = userManager.Authenticate(
               ApplicationKey, userName, password, out userToken);
            if (authenticated)
            {
                IIdentity identity = new GenericIdentity(userName);
                SASPrincipal.Attach(
                   identity, userManager, ApplicationKey, userToken, CacheRoles, CacheClaims);
            }

            LoginEventArgs loginEventArgs = new LoginEventArgs(authenticated);
            LoginEvent(this, loginEventArgs);
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            OnLogin(sender, e);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            LoginEventArgs loginEventArgs = new LoginEventArgs(false);
            LoginEvent(this, loginEventArgs);
        }

        private void txtPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                OnLogin(sender, (EventArgs) e);
            }
        }

        
        //   protected abstract IUserManager GetUserManager();
        

        
    }

    public class LoginEventArgs : EventArgs
    {
        public LoginEventArgs(bool authenticated)
        {
            Authenticated = authenticated;
        }
        public bool Authenticated { get; internal set; }
    }

}
